/* util */
import _ from 'lodash';
/* api */
import * as SettingTaskApi from '@src/api/SettingTaskApi';
import { parse } from '@src/util/querystring'
// components
// import EditorColumnDialog from './components/EditorColumnDialog';
import EditorColumnDialog from '../../components/EditorColumnDialog'
import { openAccurateTab } from '@src/util/platform';
import { PageRoutesTypeEnum } from 'pub-bbx-global/pageType/dist/enum/PageRoutesEnum'
import i18n from '@src/locales'

import { getLocalesOssUrl } from '@src/util/assets'
const settingImage1 = getLocalesOssUrl('/setting/spareparts.png')
const settingImage2 = getLocalesOssUrl('/setting/service.png')
export default {
  name: 'task_card_setting',
  data() {
    return {
      settingImage1,
      settingImage2,
      pending: false,
      loading: true,
      cardData: {},
      cardConfig: {},
      fields: [],
      fieldsList:[],
      cardType:'',
      visible: false
    }
  },
  computed: {
    // 编辑字段选项的字段
    fieldsOptions() {  
      return this.fieldsList.filter((item)=> item.enabled == 1)
    }
  },
  mounted(){
    let query = parse(window.location.search) || {};
    this.taskCardId = query.cardId;
    this.cardType = query.cardType;
    this.initCardView();
  },
  methods: {
    // 编辑列表显示字段
    onEditColumn() {
      this.$refs.editorColumnDialog.openDialog();
      this.$nextTick(()=> {
        this.$refs.editorColumnDialog.fields = _.cloneDeep(this.fieldsOptions);
      })
    },
    // 保存编辑字段
    onSaveSetSubmit(value) {
      this.fields = value.map(item => {
        return {
          ...item,
          isHidden: 0
        }
      });
    },
    onSubmit() {
      if(this.cardConfig.roleList && this.cardConfig.roleList.length == 0) return this.$message.warning(i18n.t('task.tip.spareServiceTip1'));
      if(this.cardConfig.sparePartReturn && !this.cardConfig?.sparePartReturnRoleList?.length) return this.$message.warning(i18n.t('task.tip.spareServiceTip21'));

      this.sardTaskCard();
    },
    // 返回
    back(){
      let fromId = window.frameElement.getAttribute('fromid');
      let id = window.frameElement.dataset.id
      this.$platform.closeTab(id);
      if(!this.cardType){
        // this.$platform.openTab({
        //   id: 'M_SYSTEM',
        //   title: '附加组件设置',
        //   url: '/setting/task/cardManage',
        //   reload: true,
        //   close: true,
        //   fromId
        // }); 
        openAccurateTab({
          type: PageRoutesTypeEnum.PageTaskCardManage,
          reload: true,
          fromId
        }) 
      }
    },
    // 初始化组件信息
    initCardView() {
      SettingTaskApi.getInventoryConfig({taskCardId: this.taskCardId}).then(res=>{
        const { code, result } = res;
        if(code == 0){  
          this.loading = false;
          this.cardData = result;
          this.cardConfig = result.config;
          this.fieldsList = _.cloneDeep(result.taskCardFields)
          this.fields = (result?.taskCardFields || []).filter(item => item.isHidden == 0);
        }
      }).catch(error=>({}))
    },
    // 保存备件信息配置
    sardTaskCard() {
      const params = Object.assign(this.cardData, { config:this.cardConfig }, { taskCardFields: this.fields }) 
      SettingTaskApi.modifyInventoryConfig(params).then(res=>{
        const { code, message, data } = res;
        if(code == 0){
          this.$platform.notification({
            title: i18n.t('common.base.saveSuccess'),
            type: 'success'
          }); 
          setTimeout(()=>{
            this.back()
          }, 500)
        }else{
          this.$platform.notification({
            title: message,
            type: 'error'
          }); 
        }
      }).catch(error=>({}))
    },
    onPreview() {
      this.visible = true;
    },
    onClose() {
      this.visible = false;
    }
  },
  components: {
    [EditorColumnDialog.name]: EditorColumnDialog,
  },
}